var utils = require("../../../../utils/util.js");
Page({
  data: {
    historyList: [],
    curId: -1
  },
  onShow: function () {
    let historyList = wx.getStorageSync('historyList') || [];
    this.setData({
      curId: -1,
      historyList: historyList
    });
  },
  restoryItem: function (event){
    let restoryId = event.currentTarget.dataset.restoryid;
    let historyList = wx.getStorageSync('historyList') || [];
    let itemList = wx.getStorageSync('itemList') || [];
    let restoryItem = historyList.splice(restoryId, 1);
     
    itemList = utils.dealItemList(itemList.concat(restoryItem));
    //重置id
    historyList.map(function (obj, index) {
      obj.id = index;
    });
    //修改本地存储
    wx.setStorageSync('historyList', historyList);
    wx.setStorageSync('itemList', itemList);
    this.setData({
      curId: -1,
      historyList: historyList
    });
  },
  removeItem: function (event){
    let removeId = event.currentTarget.dataset.removeid;
    let itemList = wx.getStorageSync('historyList') || [];
    itemList.splice(removeId, 1);
    //重置id
    itemList.map(function (obj, index) {
      obj.id = index;
    });
    //修改本地存储
    wx.setStorageSync('historyList', itemList);
    this.setData({
      curId: -1,
      historyList: itemList
    });
  },
  showControl: function (event) {
    var id = event.currentTarget.id;
    if (event.currentTarget.id === this.data.curId) {
      this.setData({
        curId: -1,
      });
    } else {
      this.setData({
        curId: id,
      });
    }
  }
})